package com.example.task052;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.task051.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
    String op;
    private IMyAidlInterface myAidlInterface;//AIDL接口 远程
    //    IMyAidlInterface.Stub myBinder; //返回Binder对象
    Double num1 = 0.0;
    Double num2 = 0.0;

    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myAidlInterface = IMyAidlInterface.Stub.asInterface(iBinder);
            //客户端获取ADIL接口：通过IMyAidlInterface.Stub.asInterface(iBinder)得到IMyAidlInterface对象，
            //把远程传过来的iBinder的动态代理类转换成本地类，客户端和服务端之间建立了联系
        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //第一种方法，使用datda.xml设置数据源
        Spinner spinner  = (Spinner)findViewById(R.id.spinner);
        Button button1 = findViewById(R.id.btn_calculate);
        EditText et_num1 = findViewById(R.id.et_num1);
        EditText et_num2 = findViewById(R.id.et_num2);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String[] operations = getResources().getStringArray(R.array.operations);
                op = operations[i];
//                Toast.makeText(MainActivity.this, "你选择的是"+op, Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
//          do nothings
            }
        });

        /*服务连接类，只有在Service中的onBind方法中返回一个IBinder实例
        才会在绑定的时候调用onServiceConnection回调方法，第二个参数就是OnBind方法返回的IBinder实例，可以用来传递数据*/
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
/*            绑定计算服务
                 Intent intent = new Intent();
               intent.setAction("com.example.exp05.CalculateService");*/
                num1 = Double.parseDouble(String.valueOf(et_num1.getText()));
                num2 = Double.parseDouble(String.valueOf(et_num2.getText()));
//                Intent intent = new Intent(MainActivity.this, CalculateService.class);
                ComponentName componentName = new ComponentName("com.example.task051","com.example.task051.CalculateService");
                Intent intent = new Intent();
                intent.setComponent(componentName);
                bindService(intent,sc,Context.BIND_AUTO_CREATE);
                if(myAidlInterface==null) {
                    Toast.makeText(MainActivity.this, "您还未绑定远程服务，已为您自动绑定远程服务，请重试", Toast.LENGTH_SHORT).show();
                    bindService(intent,sc, Context.BIND_AUTO_CREATE);
                }
                if(myAidlInterface!=null){
                    try {
                        double result;
                        switch (op){
                            case "+":
                                result = myAidlInterface.add(num1,num2);
                                Toast.makeText(MainActivity.this, "加的结果为:"+result, Toast.LENGTH_SHORT).show();
                                break;
                            case "-":
                                result = myAidlInterface.subtract(num1,num2);
                                Toast.makeText(MainActivity.this, "减的结果为:"+result, Toast.LENGTH_SHORT).show();
                                break;
                            case "*":
                                result = myAidlInterface.multiply(num1,num2);
                                Toast.makeText(MainActivity.this, "乘的结果为:"+result, Toast.LENGTH_SHORT).show();
                                break;
                        }

                    }catch (RemoteException e){
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}